﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    class Fireman : Unit
    {
        private List<ParticleEngine> particleEngines;
        List<Texture2D> particleTextures;
        private ParticleEngine waterSpray;
        private FireParticleEngine target;

        private bool notMoving;
        private bool turningOutFire;

        public Fireman(float X, float Y, List<ParticleEngine> particleEngines, List<Texture2D> particleTextures, int sT)
            : base(X, Y, particleTextures, sT)
        {
            this.particleEngines = particleEngines;
            this.particleTextures = particleTextures;
            this.turningOutFire = false;
            this.notMoving = true;
            texture = ResourceManager.getTexture("Fireman");
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            if (turningOutFire)
            {
                spriteBatch.Draw(ResourceManager.getTexture("ATH"),
                    new Rectangle(
                        (int)(UnitLoc.X - unitSize / 4),
                        (int)(UnitLoc.Y) - unitSize,
                        32,
                        32),
                    Color.White);
            }
        }

        private void findFire()
        {
            foreach (ParticleEngine p in particleEngines)
            {
                Vector2 diff = p.EmitterLocation - UnitLoc;
                if ((p is CrashSimul8or.FireParticleEngine) && (diff.Length() < 80))
                {
                    target = (FireParticleEngine)p;
                    turningOutFire = true;
                    Vector2 dir = p.EmitterLocation - UnitLoc + new Vector2(0, -10);
                    dir.Normalize();
                    dir *= 2f;
                    waterSpray = new WaterParticleEngine(particleTextures, UnitLoc + new Vector2((float)unitSize / 5, (float)unitSize / 4), 50, dir);
                    particleEngines.Add(waterSpray);
                    if(GameVariables.soundEffects) ResourceManager.getSound("WaterSpray").Play();
                    break;
                }
            }
        }

        private void turnOutFire()
        {
            target.flameHealth -= .001f;
            if (target.flameHealth < 0)
            {
                particleEngines.Remove(waterSpray);
                particleEngines.Remove(target);
                turningOutFire = false;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (createdTime == 0)
            {
                spawnTime = gameTime.TotalGameTime.Seconds + spawnTime;
                createdTime = gameTime.TotalGameTime.Seconds;
            }

            int timeToSpawn = spawnTime - gameTime.TotalGameTime.Seconds;

            if (timeToSpawn <= 0 && !spawned)
            {
                spawned = true;
                this.goTo(UnitDest.X, UnitDest.Y);
            }

            if (!spawned)
            {
                status = timeToSpawn.ToString();
            }
            else
            {
                float distance;
                Vector2.Distance(ref UnitDest, ref UnitLoc, out distance);
                if (distance < 2)
                {
                    notMoving = true;
                    status = "Ready";
                    UnitLoc = new Vector2(UnitDest.X, UnitDest.Y);
                    if (!turningOutFire)
                    {
                        findFire();
                    }
                    else if (notMoving)
                    {
                        turnOutFire();
                        status = "Fire";
                    }
                }
                else
                {
                    UnitLoc += (float)1.5 * (new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
                    status = "Moving";
                }
                glowParticle.Update(UnitLoc);
            }
        }

        public override void goTo(float X, float Y)
        {
            base.goTo(X, Y);
            turningOutFire = false;
            notMoving = false;
            particleEngines.Remove(waterSpray);
        }
    }
}
